package nc.ui.am.comparison.corpdept;

import nc.bs.logging.Logger;
import nc.ui.am.pub.bill.BodyOnlyCardUI;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.bill.BillEditEvent;
import nc.ui.pub.bill.BillItem;
import nc.ui.trade.base.IBillOperate;
import nc.ui.trade.bill.ICardController;
import nc.ui.trade.button.IBillButton;
import nc.ui.trade.card.CardEventHandler;
import nc.vo.am.comparison.CorpdeptVO;
import nc.vo.pub.CircularlyAccessibleValueObject;

public class CorpDeptUI extends BodyOnlyCardUI {

	private static final long serialVersionUID = 1L;

	public CorpDeptUI() {
		super();
		try {
			((CorpDeptEH) getCardEventHandler()).onBoQuery();
		} catch (Exception e) {
			showErrorMessage(e.getMessage());
			Logger.error(e.getMessage(), e);

		}
	}

	protected CardEventHandler createEventHandler() {
		return new CorpDeptEH(this, getUIControl());
	}

	protected ICardController createController() {
		return new CorpDeptCtrl();
	}

	@Override
	protected void fillDataToBodyVos(CircularlyAccessibleValueObject[] bodyVos) {

	}

	@Override
	public String getRefBillType() {

		return null;
	}

	@Override
	protected void initSelfData() {
		getBillCardPanel().getBodyPanel().getRendererVO()
				.setShowZeroLikeNull(false);

	}

	@Override
	public void setDefaultData() throws Exception {

	}

	@Override
	public boolean beforeEdit(BillEditEvent e) {
		if (CorpdeptVO.PAMDEPT_CODE.equalsIgnoreCase(e.getKey())) {
			int row = e.getRow();
			int col = getBillCardPanel().getBillModel().getBodyColByKey(
					CorpdeptVO.SELFDEF2);
			String pamcorp_code = (String) getBillCardPanel().getBillModel()
					.getValueAt(row, col);
			UIRefPane ref = (UIRefPane) ((BillItem) e.getSource())
					.getComponent();
			ref.setWhereString("( pk_corp='" + pamcorp_code + "') ");

		}
		return true;
	}

	@Override
	public void afterEdit(BillEditEvent e) {
		if (CorpdeptVO.PAMCORP_CODE.equalsIgnoreCase(e.getKey())) {
			int row = e.getRow();
			int deptcol = getBillCardPanel().getBillModel().getBodyColByKey(
					CorpdeptVO.PAMDEPT_CODE);
			int selcol = getBillCardPanel().getBillModel().getBodyColByKey(
					CorpdeptVO.SELFDEF1);
			int namecol = getBillCardPanel().getBillModel().getBodyColByKey(
					CorpdeptVO.PAMDEPT_NAME);
			this.getBillCardPanel().getBillModel().setValueAt("", row, deptcol);
			this.getBillCardPanel().getBillModel().setValueAt("", row, selcol);
			this.getBillCardPanel().getBillModel().setValueAt("", row, namecol);

		}
		getBillCardPanel().getBillModel().execLoadFormula();
	}

	@Override
	public void updateButtons() {

		if (IBillOperate.OP_EDIT == getBillOperate()) {
			getButtonManager().getButton(IBillButton.ImportBill).setEnabled(
					false);
		} else {
			getButtonManager().getButton(IBillButton.ImportBill).setEnabled(
					true);
		}
		super.updateButtons();
	}
}
